#!/bin/bash

log-debug "starting agent again..."
log-debug "bringing agent down..."
docker-down spire-agent
log-debug "starting agent again..."
docker-up spire-agent

# Check at most 30 times (with one second in between) that the agent is back up
MAXCHECKS=30
CHECKINTERVAL=1
for ((i=1;i<=MAXCHECKS;i++)); do
    log-info "checking that the agent is back up ($i of $MAXCHECKS max)..."
    docker compose logs spire-agent
    if docker compose logs spire-agent | grep "Starting Workload and SDS APIs"; then
	    exit 0
    fi
    sleep "${CHECKINTERVAL}"
done
